//
//  FSBaseViewController.m
//  FSiOSClient
//
//  Created by purlible on 2024/8/22.
//

#import "FSBaseViewController.h"
#import "FSHudManager.h"
#import "FSHomeViewController.h"

@interface FSBaseViewController () <UIGestureRecognizerDelegate>

@end

@implementation FSBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_VIEW_BG;
    
    if (@available(iOS 15.0, *))
    {
        UINavigationBarAppearance *appperance = [[UINavigationBarAppearance alloc]init];

        appperance.backgroundColor = UIColor.whiteColor;
        appperance.shadowImage = [[UIImage alloc]init];
        appperance.shadowColor = nil;
        
        [appperance setTitleTextAttributes:@{NSForegroundColorAttributeName:COLOR_333333,NSFontAttributeName:FONT_SIZE_X17}];
        
        self.navigationController.navigationBar.standardAppearance = appperance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appperance;
    }
    else
    {
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.barTintColor = UIColor.whiteColor;
        
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:COLOR_333333,NSFontAttributeName:FONT_SIZE_X17}];
    }
    
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    UIImage* barImage = [UIImage imageNamed:@"icon_back_black"];
    barImage = [barImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIBarButtonItem* barItem = [[UIBarButtonItem alloc] initWithImage:barImage style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    self.navigationItem.leftBarButtonItem = barItem;
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return self.navigationController.viewControllers.count > 1;
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    if ([NSStringFromClass([self class]) rangeOfString:@"FSIndexViewController"].location != NSNotFound ||
        [NSStringFromClass([self class]) rangeOfString:@"FSCommandViewController"].location != NSNotFound ||
        [NSStringFromClass([self class]) rangeOfString:@"FSMineViewController"].location != NSNotFound)
    {
//        [self.rdv_tabBarController setTabBarHidden:NO animated:YES];
    }
    
    [super viewDidAppear:animated];
}

- (void)viewWillAppear:(BOOL)animated
{
    if ([[self.navigationController childViewControllers] count] > 1)
    {
//        [self.rdv_tabBarController setTabBarHidden:YES animated:YES];
        [self.navigationController setNavigationBarHidden:NO animated:NO];
    }
    else
    {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    
    [super viewWillAppear:animated];
}



- (void)showLoadingHUD
{
    [[FSHudManager sharedInstance] showLoadingHUD:self.view text:@"奋力加载中..."];
    
}

- (void)showLoadingHUDInWindow
{
    UIWindow* window = self.view.window;
    if(!window)
    {
        window = [[[UIApplication sharedApplication] windows] firstObject];
    }
    
    [[FSHudManager sharedInstance] showLoadingHUD:window text:@"奋力加载中..."];
}

- (void)showLoadingHUD:(NSString*)text
{
    [[FSHudManager sharedInstance] showLoadingHUD:self.view text:text];
}

- (void)showLoadingHUDInWindow:(NSString*)text
{
    UIWindow* window = self.view.window;
    if(!window)
    {
        window = [[[UIApplication sharedApplication] windows] firstObject];
    }
    [[FSHudManager sharedInstance] showLoadingHUD:window text:text];
}

- (void) hideHUD
{
    [[FSHudManager sharedInstance] hideHUD];
}

- (void)showSuccessInWindow:(NSString *)text
{
    UIWindow* window = self.view.window;
    if(!window)
    {
        window = [[[UIApplication sharedApplication] windows] firstObject];
    }
    
    [[FSHudManager sharedInstance] showSuccess:text inView:window];
}

- (void)showSuccess:(NSString *)text {
    [[FSHudManager sharedInstance] showSuccess:text inView:self.view];
}


- (void)showError:(NSString *)text {
    [[FSHudManager sharedInstance] showError:text inView:self.view] ;
}

- (void)showErrorInWindow:(NSString *)text
{
    UIWindow* window = self.view.window;
    if(!window)
    {
        window = [[[UIApplication sharedApplication] windows] firstObject];
    }
    [[FSHudManager sharedInstance] showError:text inView:window] ;
}


- (void)showText:(NSString*)text
{
    [[FSHudManager sharedInstance] showText:text view:self.view] ;
}


- (void)showTextInWindow:(NSString*)text
{
    UIWindow* window = self.view.window;
    if(!window)
    {
        window = [[[UIApplication sharedApplication] windows] firstObject];
    }
    [[FSHudManager sharedInstance] showText:text view:window] ;
}

- (void)showNetworkError
{
    [self showText:@"您的网络好像出了问题"];
}

- (void)showNetworkErrorInWodinw
{
    [self showTextInWindow:@"您的网络好像出了问题"];
}



@end

